from typing import Dict, Optional
import time
import numpy as np
from .model_wrapper import ModelWrapper

class ModelService:
    """模型服务，管理所有模型实例"""
    
    def __init__(self):
        self.models: Dict[str, ModelWrapper] = {}
    
    def register_model(self, model_id: str, model: ModelWrapper) -> None:
        """注册一个新模型"""
        self.models[model_id] = model
    
    def get_model(self, model_id: str) -> Optional[ModelWrapper]:
        """获取已注册的模型"""
        return self.models.get(model_id)
    
    def predict(self, model_id: str, image: np.ndarray, options: Dict = None) -> Dict:
        """使用指定模型进行预测"""
        model = self.get_model(model_id)
        if model is None:
            raise ValueError(f"Model {model_id} not found")
        
        start_time = time.time()
        result = model.predict(image, options)
        report = model.generate_report(result, start_time)
        
        # 合并结果和报告
        final_result = {**report, **result}
        return final_result
